/*  Game/Army Editor for yALB - Yet Another List Builder (roster Editor for Wargames/tabletop games).
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

#ifndef ITEMSELECTOR_H
#define ITEMSELECTOR_H

#include <QDialog>
#include <QStringList>
#include <QTreeWidget>

#include "../collections/categorycollection.h"
#include "../collections/itemcollection.h"

namespace Ui {
    class ItemSelector;
}

class ItemSelector : public QDialog
{
    Q_OBJECT

public:

    explicit ItemSelector(const ItemCollection& _items, const CategoryCollection& _categories,
                          QWidget *parent = 0);
    ~ItemSelector();
    void clearAll();

    QStringList getListFromSelectedItems();

    QString getStringFromSelectedItems(const QString& currentValue);

public slots:
    void accept();

protected:
    void changeEvent(QEvent *event);

private slots:
    void on_appendItem_clicked();
    void on_subtractItem_clicked();
    void on_excludedItemTree_itemSelectionChanged();
    void on_includedItemTree_itemSelectionChanged();

    void addCategory(const GameCategory &category, const QString &key);
    void updateCategory(const QString& oldKey, const GameCategory& newCategory,
                         const QString& newKey);
    void removeCategory(const QString& key);

    void addItem(const GameItem& item, const QString &key);
    void updateItem(const QString& oldKey, const GameItem& newItem,
                         const QString& newKey);
    void removeItem(const QString& key);

    void removeAll();

private:
    Ui::ItemSelector *ui;

    QStringList addedCategories;
    QStringList addedItems;
    QStringList excludedItems;
    QString appendedString;

    QList<QString> itemOrder;
    QList<QString> categoryOrder;

    const CategoryCollection *categories; // (Variable) pointer to const CategoryCollection
    const ItemCollection *items; // (Variable) pointer to const ItemCollection

    void moveTreeWidgetItem(QTreeWidgetItem *currentItem, QTreeWidget& destination);
    void addItemToTree(const QString& itemKey, QTreeWidgetItem **categoryIncludedItem = NULL,
                       QTreeWidgetItem **categoryExcludedItem = NULL);

    void fillFromString(const QString& value);
    int findItemData(const QTreeWidget &treeWidget, const QVariant& data);

};

#endif // ITEMSELECTOR_H
